package com.cysyz.web.action

import com.cysyz.dao.BaseDao
import com.cysyz.dao.impl.InvalidIdException
import com.cysyz.entity.product.Brand
import com.cysyz.entity.product.ProductType
import com.cysyz.service.product.impl.BrandService
import com.cysyz.web.form.BrandForm
import com.cysyz.web.form.TypeForm
import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.apache.struts.action.ActionForm
import org.apache.struts.action.ActionForward
import org.apache.struts.action.ActionMapping
import org.apache.struts.actions.DispatchAction
import org.springframework.stereotype.Controller

@Controller("/contorl/producttype/manage")
class TypeManageAction extends DispatchAction{
	@Resource BrandService brandService
	@Resource BaseDao dao
	def public ActionForward addChildUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		var typeForm = form as TypeForm
		var type = new ProductType
		type.typeid = typeForm.parentId
		type.name = new String(typeForm.name.getBytes("iso-8859-1"),"utf-8")
		println(type.name)
		request.setAttribute("parent", type)
		mapping.findForward("addChild")
	}
	
	def public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		mapping.findForward("add")
	}

	def public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		var brandForm = form as BrandForm
		var Brand brand = null; 
		try {
			brand = dao.get(typeof(Brand), brandForm.brandid)
		} catch(InvalidIdException e) {
			e.printStackTrace			
		}
		request.setAttribute("brand",brand);
		mapping.findForward("edit");
	}

	def public ActionForward edit (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		var brandForm = form as BrandForm
		var brand = new Brand()
		brand.name = brandForm.name
		brand.brandid = brandForm.brandid
		if (brandForm.logoFile != null && brandForm.logoFile.fileSize > 0) {
			brandForm.logoFile.fileData
			brandService.edit(brand, brandForm.logoFile.fileName, brandForm.logoFile.fileData, request.getRealPath("/"))
		} else {
			brandService.edit(brand)
		}
		new ActionForward("/contorl/brand/list.do?page=1")
	} 
	
	def public ActionForward queryUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		mapping.findForward("query");
	}
	
	def public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		
		var typeForm = form as TypeForm
		var type = new ProductType()
		type.name = typeForm.name
		type.note = typeForm.note
		dao.save(type)
		new ActionForward("/control/producttype/list.do?page=1")
	}
	def public ActionForward addChild(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		
		var typeForm = form as TypeForm
		try {
			var parent = dao.get(typeof(ProductType), typeForm.parentId)
			var type = new ProductType()
			type.name = typeForm.name
			type.note = typeForm.note
			type.parent = parent
			dao.save(type)
			new ActionForward("/control/producttype/list.do?page=1")
		} catch (InvalidIdException e) {
			e.printStackTrace
			new ActionForward("/control/producttype/list.do?page=1")
		}
	}
}